package com.ruoyi.web.controller.invoice;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.dto.JqbhDto;
import com.ruoyi.exception.InvoiceBusinessException;
import com.ruoyi.third.base.InvoiceApiResult;
import com.ruoyi.third.entity.request.*;
import com.ruoyi.third.service.ThirdApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@Api(tags = "发票")
@RestController
@AllArgsConstructor
@RequestMapping("invoice")
public class InvoiceController {

    private final ThirdApiService thirdApiService;


    @ApiOperation(value = "发票领购信息余票查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class),
    })
    @PostMapping("invoiceLinggou")
    public InvoiceApiResult invoiceLinggou(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.invoiceLinggou(param.get("jqbh"), param.get("fplxdm"));
    }


    @ApiOperation(value = "同步登记信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class)
    })
    @PostMapping("synchronRegistInfor")
    public InvoiceApiResult synchronRegistInfor(@RequestBody @Validated JqbhDto param) {
        return thirdApiService.synchronRegistInfor(param.getJqbh());
    }


    @ApiOperation(value = "抄报状态查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "QueryCopyNewParam", value = "QueryCopyNewParam", dataTypeClass = QueryCopyNewParam.class)
    })
    @PostMapping("queryCopyNew")
    public InvoiceApiResult queryCopyNew(@RequestBody @Validated QueryCopyNewParam param) {
        return thirdApiService.queryCopyNew(param);
    }


    @ApiOperation(value = "发票同步", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceSync", value = "InvoiceSync", dataTypeClass = InvoiceSync.class)
    })
    @PostMapping("synchronization")
    public InvoiceApiResult synchronization(@RequestBody @Validated InvoiceSync param) {
        return thirdApiService.synchronization(param);
    }

    @ApiOperation(value = "发票修复", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceSync", value = "InvoiceSync", dataTypeClass = InvoiceSync.class)
    })
    @PostMapping("invoiceRepair")
    public InvoiceApiResult invoiceRepair(@RequestBody @Validated InvoiceSync param) {
        return thirdApiService.invoiceRepair(param);
    }

    @ApiOperation(value = "网上领购查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class)
    })
    @PostMapping("tickConllinfQuery")
    public InvoiceApiResult tickConllinfQuery(@RequestBody @Validated JqbhDto param) {
        return thirdApiService.tickConllinfQuery(param.getJqbh());
    }

    @ApiOperation(value = "网上领购录入", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "OnlineConllinf", value = "OnlineConllinf", dataTypeClass = OnlineConllinf.class)
    })
    @PostMapping("onlineConllinfQuery")
    public InvoiceApiResult onlineConllinfQuery(@RequestBody @Validated OnlineConllinf param) {
        return thirdApiService.onlineConllinfQuery(param);
    }

    //-----------------------------------------------------------------------------------------

    @ApiOperation(value = "获取当前未开票号", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class)
    })
    @PostMapping("obtainNoticNumber")
    public InvoiceApiResult obtainNoticNumber(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.obtainNoticNumber(param.get("jqbh"), param.get("fplxdm"));
    }


    @ApiOperation(value = "专电普开票", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceFillIn", value = "InvoiceFillIn", dataTypeClass = InvoiceFillIn.class)
    })
    @PostMapping("fillInInvoice")
    public InvoiceApiResult fillInInvoice(@RequestBody @Validated InvoiceFillIn param) {
        return thirdApiService.fillInInvoice(param);
    }

    @ApiOperation(value = "发票作废", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceVoid", value = "InvoiceVoid", dataTypeClass = InvoiceVoid.class)
    })
    @PostMapping("voidBlankInvoice")
    public InvoiceApiResult voidBlankInvoice(@RequestBody @Validated InvoiceVoid param) {
        return thirdApiService.voidBlankInvoice(param);
    }

    @ApiOperation(value = "红字信息表申请", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoicRed", value = "InvoicRed", dataTypeClass = InvoicRed.class)
    })
    @PostMapping("scarletLetterSq")
    public InvoiceApiResult scarletLetterSq(@RequestBody @Validated InvoicRed param) {
        return thirdApiService.scarletLetterSq(param);
    }


    @ApiOperation(value = "红字撤销", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sqlsh", value = "申请流水号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "xxbbh", value = "信息表编号", dataTypeClass = String.class),
    })
    @PostMapping("scarletLetterRevoke")
    public InvoiceApiResult scarletLetterRevoke(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("sqlsh")) || StrUtil.isBlank(param.get("xxbbh"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.scarletLetterRevoke(param.get("jqbh"), param.get("sqlsh"), param.get("xxbbh"));
    }


    @ApiOperation(value = "发票查询接口（查盘：用于发票修复）", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceQueryDbParam", value = "InvoiceQueryDbParam", dataTypeClass = InvoiceQueryDbParam.class)
    })
    @PostMapping("invoiceQuery")
    public InvoiceApiResult invoiceQuery(@RequestBody @Validated InvoiceQueryParam param) {
        return thirdApiService.invoiceQuery(param);
    }

    @ApiOperation(value = "发票查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceQueryDbParam", value = "InvoiceQueryDbParam", dataTypeClass = InvoiceQueryDbParam.class)
    })
    @PostMapping("invoiceQueryDb")
    public InvoiceApiResult InvoiceQueryDb(@RequestBody @Validated InvoiceQueryDbParam param) {
        return thirdApiService.InvoiceQueryDb(param);
    }

    @ApiOperation(value = "发票详情查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceQueryMaxParam", value = "InvoiceQueryMaxParam", dataTypeClass = InvoiceQueryMaxParam.class)
    })
    @PostMapping("InvoiceQueryDbDdetail")
    public InvoiceApiResult InvoiceQueryDb(@RequestBody @Validated InvoiceQueryMaxParam param) {
        return thirdApiService.InvoiceQueryDb(param);
    }

    //----------------------------------------------------------------------------------------
    @ApiOperation(value = "红字信息表查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceRedQueryParam", value = "InvoiceRedQueryParam", dataTypeClass = InvoiceRedQueryParam.class)
    })
    @PostMapping("queryHzxxb")
    public InvoiceApiResult queryHzxxb(@RequestBody @Validated InvoiceRedQueryParam param) {
        return thirdApiService.queryHzxxb(param);
    }

    @ApiOperation(value = "抄报接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class)
    })
    @PostMapping("copyNew")
    public InvoiceApiResult copyNew(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.copyNew(param.get("jqbh"), param.get("fplxdm"));
    }

    @ApiOperation(value = "反写接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class)
    })
    @PostMapping("reverseWrite")
    public InvoiceApiResult reverseWrite(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.reverseWrite(param.get("jqbh"), param.get("fplxdm"));
    }


    @ApiOperation(value = "红字信息表上传", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sqdh", value = "通知单编号", dataTypeClass = String.class)
    })
    @PostMapping("hzxxbSc")
    public InvoiceApiResult hzxxbSc(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("sqdh"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.hzxxbSc(param.get("jqbh"), param.get("sqdh"));
    }

    @ApiOperation(value = "票种核定接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "nsrsbh", value = "纳税人识别号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class),
    })
    @PostMapping("queryinvoiceTypeCode")
    public InvoiceApiResult queryinvoiceTypeCode(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("nsrsbh")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.queryinvoiceTypeCode(param.get("jqbh"), param.get("nsrsbh"), param.get("fplxdm"));
    }


    @ApiOperation(value = "发票统计查询接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ReportQueryParam", value = "ReportQueryParam", dataTypeClass = ReportQueryParam.class)
    })
    @PostMapping("reportQuery")
    public InvoiceApiResult reportQuery(@RequestBody @Validated ReportQueryParam param) {
        return thirdApiService.reportQuery(param);
    }

    @ApiOperation(value = "票种核定接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class),
    })
    @PostMapping("inputHzxxbInvoice")
    public InvoiceApiResult inputHzxxbInvoice(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("file")) || StrUtil.isBlank(param.get("fplxdm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.inputHzxxbInvoice(param.get("jqbh"), param.get("file"), param.get("fplxdm"));
    }

}
